//
//  testjsbind.cpp
//  SPJSBind
//
//  Created by 云 浮 on 12-5-31.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#include "testjsbind.h"

// static member property init.
INITIALIZE_JSCLASS(CJSProxy)

INITIALIZE_JSFUNCTION_BEGIN(CJSProxy)
INITIALIZE_JSFUNCTION(print_int, 0)
INITIALIZE_JSFUNCTION_END

INITIALIZE_JSPROPERTY_GET_BEGIN(CJSProxy)
INITIALIZE_JSPROPERTY_GET(TEST_BOOL, m_bool)
INITIALIZE_JSPROPERTY_GET(TEST_INT, m_int)
INITIALIZE_JSPROPERTY_GET(TEST_FLOAT, m_float)
INITIALIZE_JSPROPERTY_GET(TEST_DOUBLE, m_double)
INITIALIZE_JSPROPERTY_GET(TEST_STRING, m_pchar)
INITIALIZE_JSPROPERTY_GET(TEST_LPVOID, m_pvoid)
INITIALIZE_JSPROPERTY_GET(TEST_LPCUSTOM, m_pcustom)
INITIALIZE_JSPROPERTY_GET_END

INITIALIZE_JSPROPERTY_SET_BEGIN(CJSProxy)
INITIALIZE_JSPROPERTY_SET(TEST_BOOL, m_bool, bool)
INITIALIZE_JSPROPERTY_SET(TEST_INT, m_int, int)
INITIALIZE_JSPROPERTY_SET(TEST_FLOAT, m_float, float)
INITIALIZE_JSPROPERTY_SET(TEST_DOUBLE, m_double, double)
INITIALIZE_JSPROPERTY_SET(TEST_STRING, m_pchar, char*)
INITIALIZE_JSPROPERTY_SET(TEST_LPVOID, m_pvoid, void*)
INITIALIZE_JSPROPERTY_SET(TEST_LPCUSTOM, m_pcustom, CJSProxy*)
INITIALIZE_JSPROPERTY_SET_END

INITIALIZE_JSPROPERTY_BEGIN(CJSProxy)
INITIALIZE_JSPROPERTY(TEST_BOOL, m_bool)
INITIALIZE_JSPROPERTY(TEST_INT, m_int)
INITIALIZE_JSPROPERTY(TEST_FLOAT, m_float)
INITIALIZE_JSPROPERTY(TEST_DOUBLE, m_double)
INITIALIZE_JSPROPERTY(TEST_STRING, m_pchar)
INITIALIZE_JSPROPERTY(TEST_LPVOID, m_pvoid)
INITIALIZE_JSPROPERTY(TEST_LPCUSTOM, m_pcustom)
INITIALIZE_JSPROPERTY_END

CJSProxy::CJSProxy() :
m_bool(false), m_char(0), m_short(0), m_int(1), m_float(2.01f), m_double(3.02), m_pchar(0), m_pvoid(0), m_pcustom(0)
{
    INITIALIZE_JSOBJECT(CJSProxy)
}

CJSProxy::~CJSProxy()
{
//    DELETE_JSOBJECT(CJSProxy)
}

void CJSProxy::print_int()
{
    printf("==== print info ==== S\n") ;
    printf("[%d]\n", m_bool) ;
    printf("[%d]\n", m_int) ;
    printf("[%f]\n", m_float) ;
    printf("[%f]\n", m_double) ;
    printf("[%s]\n", (char*)m_pchar) ;
    printf("==== print info ==== E\n") ;
}


INITIALIZE_JSCLASS(base)

INITIALIZE_JSFUNCTION_BEGIN(base)
INITIALIZE_JSFUNCTION_END

//INITIALIZE_JSPROPERTY_GET_BEGIN(base)
//INITIALIZE_JSPROPERTY_GET(TEST_BASE, m_nbase)
//INITIALIZE_JSPROPERTY_GET_END

JSBool base::JSGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) {
    base* p ;
    __ASSERT(p = (base*)JS_GetPrivate(cx, obj)) ;
    switch (JSVAL_TO_INT(id)) {
        case TEST_BASE: { *vp = _CONVERT_C_TO_JS(cx, p->m_nbase) ; } break ;
        default: break ;
    }
    return JS_TRUE ;
}

INITIALIZE_JSPROPERTY_SET_BEGIN(base)
INITIALIZE_JSPROPERTY_SET(TEST_BASE, m_nbase, int)
INITIALIZE_JSPROPERTY_SET_END

INITIALIZE_JSPROPERTY_BEGIN(base)
INITIALIZE_JSPROPERTY(TEST_BASE, m_nbase)
INITIALIZE_JSPROPERTY_END

base::base()
{
    m_nbase = 11 ;
    INITIALIZE_JSOBJECT(base) ;
}

///////////////////////////////////////////////////////////
INITIALIZE_JSCLASS(child)

INITIALIZE_JSFUNCTION_BEGIN(child)
INITIALIZE_JSFUNCTION_END

INITIALIZE_JSPROPERTY_GET_BEGIN(child)
INITIALIZE_JSPROPERTY_GET(TEST_CHILD, m_nchild)
INITIALIZE_JSPROPERTY_GET_END

INITIALIZE_JSPROPERTY_SET_BEGIN(child)
INITIALIZE_JSPROPERTY_SET(TEST_CHILD, m_nchild, int)
INITIALIZE_JSPROPERTY_SET_END

INITIALIZE_JSPROPERTY_BEGIN(child)
INITIALIZE_JSPROPERTY(TEST_CHILD, m_nchild)
INITIALIZE_JSPROPERTY_END


child::child()
{
    m_nchild = 12 ;

//    m_pJSObject = JS_NewObject(s_pContext, &CJSProxyClass, 0, ((base*)this)->m_pJSObject) ;
    INITIALIZE_HAS_PARENT_JSOBJECT(child, base)
}

child::~child()
{
}







































